<?php

include_once('admin.php');

/**
 * The User Management Class provides functions to Add, Remove or Modify Users (Requires Super User access)
 * @package admin
 */
class user_management extends admin
{
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		
		// Redirect if not logged in as admin
		if ( !$this->session->userdata('CurrentUser') || !$this->session->userdata('CurrentUser')->su_access )
			redirect('authenticate/login');
		
		// Load the form helper
		$this->load->helper('form');
		
		// Load the form validation library
		$this->load->library('form_validation');
		
		// Load models
		$this->load->model('users_model');
    }
	
	/**
     * Displays a list of all users
     */
	public function index( )
	{
		// Get contents of User Table
		$this->data['users'] = $this->users_model->getAllUsers( );
	
		// Load the "view_volume" view
		$this->data['main_content'] = $this->load->view('admin/user_management/search', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
	
	/**
     * Delete a user
	 * @param $username Username of user to delete
     */
	public function delete( $username )
	{
		// Delete user
		$this->users_model->deleteUser( $username );
	
		// Redirect back to previous search
		redirect( 'admin/user_management/' );
	}
	
	/**
     * Modify a user
	 * @param $username Username of user to modify
     */
	public function modify( $username )
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('admin/user_management/modify'))
			{
				// Get the data to insert
				$form_data = array(	'username' => $this->input->post('username'),
									'password' => sha1($this->input->post('password')),
									'staff_access' => $this->input->post('staff_access'),
									'admin_access' => $this->input->post('admin_access'),
									'su_access' => $this->input->post('su_access'));
			
				// Update volumes where access_id = $access_id with the values retrieved from the form
				$this->users_model->updateUser( $form_data['username'], $form_data );
			
				redirect('admin/user_management/');
			}
		}
	
		// Get user details
		$this->data['user'] = $this->users_model->getUserByUsername($username)->row();
	
		// Load the "add" view
		$this->data['main_content'] = $this->load->view('admin/user_management/modify_user', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);	
	}
	
	/**
     * Add a user
     */
	public function add( )
	{
		// If 'POST' data exists (i.e. the submit button was clicked)
		if ( $this->input->post() )
		{
			// If form validation succeeds (All fields correct)
			if ($this->form_validation->run('admin/user_management/add'))
			{
				// Get the data to insert
				$form_data = array(	'username' => $this->input->post('username'),
									'password' => sha1($this->input->post('password')),
									'staff_access' => $this->input->post('staff_access'),
									'admin_access' => $this->input->post('admin_access'),
									'su_access' => $this->input->post('su_access'));
			
				// Update volumes where access_id = $access_id with the values retrieved from the form
				$this->users_model->addUser( $form_data );
			
				redirect('admin/user_management/');
			}
		}
	
		// Load the "add" view
		$this->data['main_content'] = $this->load->view('admin/user_management/add_user', $this->data, true);
		
		// Load $this->data['main_content'] in the main template
		$this->load->view($this->template, $this->data);
	}
}

?>